package com.flaremars.picker.adapters;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.flaremars.picker.R;
import com.flaremars.picker.entities.LeafCard;
import com.flaremars.picker.utils.StringUtils;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * @author FlareMars on 2016/3/12
 * 图鉴页面 RecylerView Adapter
 */
public class LeafCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);

    private Context context;
    private List<LeafCard> data;
    private OnCardItemClickListener listener;

    private static final int ITEM_TYPE_NO_COLLECTED = 0;
    private static final int ITEM_TYPE_COLLECTED = 1;

    public interface OnCardItemClickListener {
        void onCardItemClickListener(LeafCard item);
    }

    public LeafCardAdapter(Context context, List<LeafCard> data,OnCardItemClickListener listener) {
        this.context = context;
        this.data = data;
        this.listener = listener;
    }

    public void setOnCardItemClickListener(OnCardItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_NO_COLLECTED) {
            return new NoCollectedViewHolder(LayoutInflater.from(context).inflate(R.layout.item_view_leaf_card_nocollected,parent,false));
        } else {
            return new CollectedViewHolder(LayoutInflater.from(context).inflate(R.layout.item_view_leaf_card,parent,false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final LeafCard item = data.get(position);
        if (holder instanceof NoCollectedViewHolder) {
            NoCollectedViewHolder noCollectedViewHolder = (NoCollectedViewHolder) holder;
            noCollectedViewHolder.tipsTv.setText(item.getTips());
            Picasso.with(context).load(item.getImageUrl()).placeholder(R.mipmap.ic_launcher).into(noCollectedViewHolder.image);
            noCollectedViewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onCardItemClickListener(item);
                    }
                }
            });
        } else {
            CollectedViewHolder collectedViewHolder = (CollectedViewHolder) holder;
            Picasso.with(context).load(item.getImageUrl()).placeholder(R.mipmap.ic_launcher).into(collectedViewHolder.image);
            collectedViewHolder.placeTv.setText(item.getCollectedPlace());
            collectedViewHolder.timeTv.setText(DATE_FORMAT.format(item.getCollectedTime()));
            collectedViewHolder.nameTv.setText(item.getCardName());
            collectedViewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onCardItemClickListener(item);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return StringUtils.INSTANCE.isEmpty(data.get(position).getCardName()) ? ITEM_TYPE_NO_COLLECTED : ITEM_TYPE_COLLECTED;
    }

    private class NoCollectedViewHolder extends RecyclerView.ViewHolder {

        private CardView rootView;
        private ImageView image;
        private TextView tipsTv;

        public NoCollectedViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.iv_leaf_image);
            tipsTv = (TextView) itemView.findViewById(R.id.tv_leaf_tips);
            rootView = (CardView) itemView.findViewById(R.id.root_cardview);
        }
    }

    private class CollectedViewHolder extends RecyclerView.ViewHolder {

        private CardView rootView;
        private ImageView image;
        private TextView nameTv;
        private TextView timeTv;
        private TextView placeTv;

        public CollectedViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.iv_leaf_image);
            nameTv = (TextView) itemView.findViewById(R.id.tv_leaf_name);
            timeTv = (TextView) itemView.findViewById(R.id.tv_card_collected_time);
            placeTv = (TextView) itemView.findViewById(R.id.tv_card_collected_place);
            rootView = (CardView) itemView.findViewById(R.id.root_cardview);
        }
    }
}
